//
//  JWKStatusViewModel.swift
//  Weibo
//
//  Created by 姜维克 on 16/5/25.
//  Copyright © 2016年 姜维克. All rights reserved.
//

import UIKit

class JWKStatusViewModel: NSObject {
    
    var status : JWKStatus?
    
    
    var iconUrl : NSURL?
    
    var verifiedImage : UIImage?
    
    var mbrankImage : UIImage? = UIImage(named: "common_icon_membership_expired")
    
    //发布时间---要设置为计算型属性--每次都会来调用get方法,实时显示发布时间
    var created_at:String?{
        return dealCreated_at(status?.created_at ?? "")
    }
    
    //来源
    var source : String?
    
    //statusTabbar元素
    
    //转发文案
    var reposts_countStr:String = "转发"
    //评论数
    var comments_countStr:String = "评论"
    //表态数
    var attitudes_countStr:String = "赞"
    
    
    init(status:JWKStatus) {
        super.init()
        self.status = status
        setupUI()
    }
    
    
    func setupUI(){
        
        let usermodel = status?.user
        
        //iconimageView头像url
        iconUrl = NSURL(string: usermodel?.profile_image_url ?? "")
        
        //verifiedImageView
        //MARK:verifiedImage 为nil,图片框就不显示
        let verifiedType : Bool  = (usermodel?.verified)!
        
        if verifiedType {
            
            verifiedImage = UIImage(named: "avatar_vip")
        }
        
        //mbrankImageView等级视图
        //拼接图片名
        if usermodel?.mbrank > 0 && usermodel?.mbrank < 7{
            //是vip
            let mbrankImageName = "common_icon_membership_level" + "\((usermodel?.mbrank)!)"
            
            mbrankImage = UIImage(named: mbrankImageName)
        }
        
        
        //获取created_at字符串--计算型属性
//        created_at = dealCreated_at(status?.created_at ?? "")
        
        //获取来源
        dealSource(status?.source ?? "")
        
        
        //设置statusTabbar文案
        reposts_countStr = dealStatusTabar(status?.reposts_count ?? 0, defaultText: "转发")
        comments_countStr = dealStatusTabar(status?.comments_count ?? 0, defaultText: "评论")
        attitudes_countStr = dealStatusTabar(status?.attitudes_count ?? 0, defaultText: "赞")
        
    }
    
    
    //设置发布时间
    func dealCreated_at(created_at : String)->String{
        
//        把日期时间Fri May 27 11:30:11 +0800 2016 转为日期		
        let creatDate = NSDate.stringToDate(created_at)
        
        //在把日期转换成不同的日期字符串
        let dateStr = creatDate.dateToString()
        
        return dateStr
    }
    
    //设置来源source
    func dealSource(source : String){
        
//        <a href="http://app.weibo.com/t/feed/ld7Z2" rel="nofollow">凤凰新闻客户端</a>
//        <a href="http://weibo.com/" rel="nofollow">微博 weibo.com</a>
        let startFlag = ">"
        let endFlag = "</a>"
        
        
        
        let startRange = source.rangeOfString(startFlag)
        let endRange = source.rangeOfString(endFlag)
        
        if let start = startRange , end = endRange {
            let range = start.endIndex..<end.startIndex
            self.source = source.substringWithRange(range)
        }
        
        //如果没有来源就不赋值
        
    }
    
    //设置statusTabbar文案
    func dealStatusTabar(count : Int, defaultText : String)->String{
    
        //如果数量=0,就用默认文字 文案
        if count >= 10000 {
            return "\(Double(count/1000)/10)万"
        }else if count > 0{
            return "\(count)"
        }else{
            return defaultText
        }
        
        //如果0<数量<10000 就用数量做文案
        
        //如果数量>=10000 就用多少万做文案
    }
}
